package com.tanhua.sso.controller;

import com.tanhua.common.pojo.User;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import com.tanhua.sso.vo.LoginResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> map) {
        //获取手机和验证码
        String phone = map.get("phone");
        System.out.println(phone);
        String code = map.get("verificationCode");
        System.out.println(code);
        LoginResult result = userService.login(phone, code);
        System.out.println(result);
        if (result != null) {

            //登录成功
            return ResponseEntity.ok(result);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("0000002").errMessage("登陆失败或用户已经被冻结").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryUserByToken(token);
    }
}
